package app.server.project.main.service.db.entity.statics;

import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "statistics_active")
@TableName("statistics_active")
@Comment(value = "用户总体活跃度表")
public class StaticsActiveEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("num")
    @Comment("活跃用户")
    @Column(name = "num")
    private long num;


    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDateTime createTime;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static StaticsActiveEntity of(long id, long num, LocalDateTime start) {
        var ret = new StaticsActiveEntity();
        ret.setId(id);
        ret.setSynDb(true);
        ret.setNum(num);
        ret.setCreateTime(start);
        return ret;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(StaticsActiveEntity::getCreateTime, this);
    }

    public void setNum(long num) {
        this.num = num;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(StaticsActiveEntity::getNum, this);
    }

}
